package top.mcwebsite.playandroid.business.message.impl.ui

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.lib.router.annotation.Route
import top.mcwebsite.playandroid.business.message.impl.vm.MessageIntent
import top.mcwebsite.playandroid.business.message.impl.vm.MessageUiEffect
import top.mcwebsite.playandroid.business.message.impl.vm.MessageTab
import top.mcwebsite.playandroid.business.message.impl.vm.MessageViewModel
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.lib.common.ext.encodeURL
import top.mcwebsite.playandroid.strings.Str

@Route(RouteConfig.Message.PATH)
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MessagePage() {
    val viewModel: MessageViewModel = viewModel()
    val uiState by viewModel.uiStateFlow.collectAsState()
    viewModel.HandleUiEffect {
        when (it) {
            is MessageUiEffect.GoArticleDetailPageUiEffect -> {
                EasyRouter.navigate(RouteConfig.ArticleDetail.PATH + "/${it.messageModel.fullLink.encodeURL()}")
            }
        }
    }
    Scaffold(topBar = {
        MessageTopBar(uiState.messageTab, onBackClick = { EasyRouter.popBackStack() }) {
            viewModel.sendIntent(MessageIntent.MessageTabChangeIntent(it))
        }
    }) {
        val pagerState = rememberPagerState() {
            2
        }
        LaunchedEffect(uiState.messageTab) {
            pagerState.animateScrollToPage(
                when (uiState.messageTab) {
                    MessageTab.New -> 0
                    MessageTab.History -> 1
                }
            )
        }
        LaunchedEffect(Unit) {
            snapshotFlow {
                pagerState.currentPage
            }.collect { curPage ->
                if (curPage == 0) {
                    viewModel.sendIntent(MessageIntent.MessageTabChangeIntent(MessageTab.New))
                } else if (curPage == 1) {
                    viewModel.sendIntent(MessageIntent.MessageTabChangeIntent(MessageTab.History))
                }
            }
        }
        HorizontalPager(
            state = pagerState,
            modifier = Modifier.padding(it)
        ) { page ->
            MessageContent(
                viewModel = viewModel,
                messageTab = if (page == 0) MessageTab.New else MessageTab.History
            )
        }
    }
}


@Composable
fun MessageTopBar(
    messageTab: MessageTab,
    onBackClick: () -> Unit,
    onTabClick: (MessageTab) -> Unit,
) {
    Box(
        contentAlignment = Alignment.CenterStart
    ) {
        IconButton(onClick = { onBackClick() }) {
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = stringResource(id = Str.back))
        }
        Row(
            verticalAlignment = Alignment.CenterVertically
        ) {
            Box(
                modifier = Modifier.weight(1f), contentAlignment = Alignment.CenterEnd
            ) {

                Text(
                    text = stringResource(id = Str.new_message),
                    color = LocalContentColor.current.copy(
                        if (messageTab == MessageTab.New) 1f else 0.5f
                    ),
                    modifier = Modifier.clickable { onTabClick(MessageTab.New) }
                )
            }
            Spacer(modifier = Modifier.width(20.dp))
            Box(
                modifier = Modifier.weight(1f), contentAlignment = Alignment.CenterStart
            ) {
                Text(
                    text = stringResource(id = Str.history_message),
                    color = LocalContentColor.current.copy(
                        if (messageTab == MessageTab.History) 1f else 0.5f
                    ),
                    modifier = Modifier.clickable { onTabClick(MessageTab.History) }
                )
            }
        }
    }
}